import json
import time
from concurrent.futures import ThreadPoolExecutor
import flask

# 创建一个全局的pool
pool = ThreadPoolExecutor()

app = flask.Flask(__name__)


def read_file():
    time.sleep(0.1)
    return "file_read result"


def read_db():
    time.sleep(0.2)
    return "db_read result"


def read_api():
    time.sleep(0.6)
    return "api_read result"


@app.route("/")
def index():
    # 使用submit()函数返回的是Future对象
    result_file = pool.submit(read_file)
    result_db = pool.submit(read_db)
    result_api = pool.submit(read_api)
    return json.dumps({
        "result_file": result_file.result(),
        "result_db": result_db.result(),
        "result_api": result_api.result()
    })


if __name__ == '__main__':
    # 启动flask 服务
    app.run()